package com.qingke.blog.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qingke.blog.dao.BlogDAO;

@WebServlet("/like")
public class LikeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private BlogDAO dao=new BlogDAO();
    public LikeServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int userId=Integer.parseInt(request.getParameter("userId"));
		int articleId=Integer.parseInt(request.getParameter("articleId"));
		System.out.println("user id:"+userId);
		System.out.println("article id:"+articleId);
		boolean isSuccess=false;
		if(dao.isLikeExist(userId, articleId)){
			isSuccess=dao.deleteLike(userId, articleId);
			if(isSuccess){
				response.getWriter().write("cancel");
			}
		}
		else{
			isSuccess=dao.insertLike(userId, articleId);
			if(isSuccess){
				response.getWriter().write("add");
			}
			
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
